//
//  HXDWelcomeViewController.swift
//  weibo
//
//  Created by huxuedong on 15/12/8.
//  Copyright © 2015年 huxuedong. All rights reserved.
//

import UIKit
import SDWebImage

class HXDWelcomeViewController: UIViewController {
    
    private lazy var bgImageView: UIImageView = {
        let image = UIImage(named: "ad_background")
        return UIImageView(image: image)
    }()
    
    private lazy var headImageView: UIImageView = {
        let headImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        headImageView.sd_setImageWithURL(NSURL(string: HXDUserAccountViewModel.shareInstance.userAccount!.avatar_large!)!, placeholderImage: UIImage(named: "avatar_default_big"))
        headImageView.cornerRadius = 45
        return headImageView
    }()
    
    private lazy var welcomeLabel: UILabel = {
        let welcomeLabel = UILabel()
        welcomeLabel.text = "欢迎回来"
        welcomeLabel.textColor = UIColor.darkGrayColor()
        welcomeLabel.font = UIFont.systemFontOfSize(18)
        return welcomeLabel
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        view = bgImageView
        setupUI()
    }
    
    private func setupUI() {
        view.addSubview(headImageView)
        view.addSubview(welcomeLabel)
        headImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView)
            make.top.equalTo(headImageView.snp_bottom).offset(15)
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        headImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(view).offset(100)
        }
        welcomeLabel.alpha = 0
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        NSNotificationCenter.defaultCenter().postNotificationName("switchVC", object: nil)
                })
        }
    }

}
